﻿var tools = {
    initPickColor: function () {
        $('.pick-class-label').click(function () {
            var new_class = $(this).attr('new-class');
            var old_class = $('#display-buttons').attr('data-class');
            var display_div = $('#display-buttons');
            if (display_div.length) {
                var display_buttons = display_div.find('.btn');
                display_buttons.removeClass(old_class);
                display_buttons.addClass(new_class);
                display_div.attr('data-class', new_class);
            }
        });
    },
    checkFullPageBackgroundImage: function () {
        $page = $('.full-page');
        image_src = $page.data('image');
        if (image_src !== undefined) {
            image_container = '<div class="full-page-background" style="background-image: url(' + image_src + ') "/>'
            $page.append(image_container);
        }
    },
    initFormExtendedDatetimepickers: function () {
        $('.datetimepicker').datetimepicker({
            icons: {
                time: "fa fa-clock-o",
                date: "fa fa-calendar",
                up: "fa fa-chevron-up",
                down: "fa fa-chevron-down",
                previous: 'fa fa-chevron-left',
                next: 'fa fa-chevron-right',
                today: 'fa fa-screenshot',
                clear: 'fa fa-trash',
                close: 'fa fa-remove'
            }
        });
        $('.datepicker').datetimepicker({
            format: 'YYYY-MM-DD',
            icons: {
                time: "fa fa-clock-o",
                date: "fa fa-calendar",
                up: "fa fa-chevron-up",
                down: "fa fa-chevron-down",
                previous: 'fa fa-chevron-left',
                next: 'fa fa-chevron-right',
                today: 'fa fa-screenshot',
                clear: 'fa fa-trash',
                close: 'fa fa-remove'
            }
        });
        $('.monthpicker').datetimepicker({
            format: 'YYYY-MM',
            icons: {
                time: "fa fa-clock-o",
                date: "fa fa-calendar",
                up: "fa fa-chevron-up",
                down: "fa fa-chevron-down",
                previous: 'fa fa-chevron-left',
                next: 'fa fa-chevron-right',
                today: 'fa fa-screenshot',
                clear: 'fa fa-trash',
                close: 'fa fa-remove'
            }
        });
        $('.timepicker').datetimepicker({
            //format: 'H:mm',    
            // use this format if you want the 24hours timepicker
            format: 'h:mm A', //use this format if you want the 12hours timpiecker with AM/PM toggle
            icons: {
                time: "fa fa-clock-o",
                date: "fa fa-calendar",
                up: "fa fa-chevron-up",
                down: "fa fa-chevron-down",
                previous: 'fa fa-chevron-left',
                next: 'fa fa-chevron-right',
                today: 'fa fa-screenshot',
                clear: 'fa fa-trash',
                close: 'fa fa-remove'
            }
        });
    },
    initFullCalendar: function () {
        $calendar = $('#fullCalendar');

        today = new Date();
        y = today.getFullYear();
        m = today.getMonth();
        d = today.getDate();

        $calendar.fullCalendar({
            viewRender: function (view, element) {
                // We make sure that we activate the perfect scrollbar when the view isn't on Month
                if (view.name != 'month') {
                    $(element).find('.fc-scroller').perfectScrollbar();
                }
            },
            header: {
                left: 'title',
                center: 'month,agendaWeek,agendaDay',
                right: 'prev,next,today'
            },
            defaultDate: today,
            selectable: true,
            selectHelper: true,
            views: {
                month: { // name of view
                    titleFormat: 'MMMM YYYY'
                    // other view-specific options here
                },
                week: {
                    titleFormat: " MMMM D YYYY"
                },
                day: {
                    titleFormat: 'D MMM, YYYY'
                }
            },

            select: function (start, end) {

                // on select we show the Sweet Alert modal with an input
                swal({
                    title: 'Create an Event',
                    html: '<div class="form-group">' +
						'<input class="form-control" placeholder="Event Title" id="input-field">' +
						'</div>',
                    showCancelButton: true,
                    confirmButtonClass: 'btn btn-success',
                    cancelButtonClass: 'btn btn-danger',
                    buttonsStyling: false
                }).then(function (result) {

                    var eventData;
                    event_title = $('#input-field').val();

                    if (event_title) {
                        eventData = {
                            title: event_title,
                            start: start,
                            end: end
                        };
                        $calendar.fullCalendar('renderEvent', eventData, true); // stick? = true
                    }

                    $calendar.fullCalendar('unselect');

                });
            },
            editable: true,
            eventLimit: true, // allow "more" link when too many events

            // color classes: [ event-blue | event-azure | event-green | event-orange | event-red ]
            events: [{
                title: 'All Day Event',
                start: new Date(y, m, 1),
                className: 'event-default'
            },
				{
				    id: 999,
				    title: 'Repeating Event',
				    start: new Date(y, m, d - 4, 6, 0),
				    allDay: false,
				    className: 'event-rose'
				},
				{
				    id: 999,
				    title: 'Repeating Event',
				    start: new Date(y, m, d + 3, 6, 0),
				    allDay: false,
				    className: 'event-rose'
				},
				{
				    title: 'Meeting',
				    start: new Date(y, m, d - 1, 10, 30),
				    allDay: false,
				    className: 'event-green'
				},
				{
				    title: 'Lunch',
				    start: new Date(y, m, d + 7, 12, 0),
				    end: new Date(y, m, d + 7, 14, 0),
				    allDay: false,
				    className: 'event-red'
				},
				{
				    title: 'Md-pro Launch',
				    start: new Date(y, m, d - 2, 12, 0),
				    allDay: true,
				    className: 'event-azure'
				},
				{
				    title: 'Birthday Party',
				    start: new Date(y, m, d + 1, 19, 0),
				    end: new Date(y, m, d + 1, 22, 30),
				    allDay: false,
				    className: 'event-azure'
				},
				{
				    title: 'Click for Creative Tim',
				    start: new Date(y, m, 21),
				    end: new Date(y, m, 22),
				    url: 'http://www.creative-tim.com/',
				    className: 'event-orange'
				},
				{
				    title: 'Click for Google',
				    start: new Date(y, m, 21),
				    end: new Date(y, m, 22),
				    url: 'http://www.creative-tim.com/',
				    className: 'event-orange'
				}
            ]
        });
    },
    showNotification: function (from, align, msg, type) {
        //type = ['', 'info', 'success', 'warning', 'danger', 'rose', 'primary'];
        $.notify({
            icon: "notifications",
            message: msg
        }, {
            type: (type || "black"),
            timer: 2000,
            placement: {
                from: from,
                align: align
            }
        });
    }
};

//去除空格 
String.prototype.Trim = function () {
    return this.replace(/\s+/g, "");
}


var common = {
    GetUrlRelativePath: function (url) {
        var arrUrl = url.split("//");
        var start = arrUrl[1].indexOf("/");
        var relUrl = arrUrl[1].substring(start);
        if (relUrl.indexOf("?") != -1) {
            relUrl = relUrl.split("?")[0];
        }
        return relUrl;
    },
    //去除换行 
    clearBr: function (key) {
        key = key.replace(/<\/?.+?>/g, "");
        key = key.replace(/[\r\n]/g, "");
        return key;
    },
    //去除左侧空格 
    lTrim: function (str) {
        return str.replace(/^\s*/g, "");
    },
    //去右空格 
    rTrim: function (str) {
        return str.replace(/\s*$/g, "");
    },
    //去掉字符串两端的空格 
    trim: function (str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    },
    //去除字符串中间空格 
    cTim: function (str) {
        return str.replace(/\s/g, '');
    },
    //是否为由数字组成的字符串 
    is_digitals: function (str) {
        var reg = /^[0-9]*$/; //匹配整数 
        return reg.test(str);
    },
    //格式化日期格式
    dateForamtToString: function (time, format) {
        var t = new Date(time);
        if (t.getFullYear() === 1) return '--';
        var tf = function (i) {
            return (i < 10 ? '0' : '') + i
        };
        return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
            switch (a) {
                case 'yyyy':
                    return tf(t.getFullYear());
                    break;
                case 'MM':
                    return tf(t.getMonth() + 1);
                    break;
                case 'mm':
                    return tf(t.getMinutes());
                    break;
                case 'dd':
                    return tf(t.getDate());
                    break;
                case 'HH':
                    return tf(t.getHours());
                    break;
                case 'ss':
                    return tf(t.getSeconds());
                    break;
            }
        })
    },
    //格式化人民币
    FormatMoney: function (num) {
        num = num.toString().replace(/\$|\,/g, '');
        if (isNaN(num))
            num = "0";
        sign = (num == (num = Math.abs(num)));
        num = Math.floor(num * 100 + 0.50000000001);
        cents = num % 100;
        num = Math.floor(num / 100).toString();
        if (cents < 10)
            cents = "0" + cents;
        for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3) ; i++)
            num = num.substring(0, num.length - (4 * i + 3)) + ',' +
			num.substring(num.length - (4 * i + 3));
        return '¥' + (((sign) ? '' : '-') + num + '.' + cents);
    },
    table_rowspan: function (table_id, table_colnum) {
        table_firsttd = "";
        table_currenttd = "";
        table_SpanNum = 0;
        table_Obj = $(table_id + " tr td:nth-child(" + table_colnum + ")");

        table_Obj.each(function (i) {
            if (i == 0) {
                table_firsttd = $(this);
                table_SpanNum = 1;
            } else {
                table_currenttd = $(this);
                if (table_firsttd.text() == table_currenttd.text()) {
                    if (table_firsttd.text() != "") {
                        table_SpanNum++;
                        table_currenttd.hide();
                        table_firsttd.attr("rowSpan", table_SpanNum);
                    }
                } else {
                    table_firsttd = $(this);
                    table_SpanNum = 1;
                }
            }
        });
    }
}

var lang = {
    "show": "\u663e\u793a",
    "sProcessing": "<img src='../../layer/skin/default/loading-0.gif' />",
    "sLengthMenu": "\u6bcf\u9875 _MENU_ \u6761\uff0c",
    "sZeroRecords": "<h4>\u6ca1\u6709\u5339\u914d\u7ed3\u679c</h4>",
    "sInfo": "\u5f53\u524d\u7b2c _START_ \u81f3 _END_ \u6761\uff0c\u5171 _TOTAL_ \u6761",
    "sInfoEmpty": "\u5f53\u524d\u7b2c 0 \u81f3 0 \u9879\uff0c\u5171 0 \u9879",
    "sInfoFiltered": "(\u7531 _MAX_ \u6761\u7ed3\u679c\u8fc7\u6ee4)",
    "sInfoPostFix": "",
    "sSearch": "\u641c\u7d22:",
    "sUrl": "",
    "sLoadingRecords": "\u8f7d\u5165\u4e2d...",
    "sInfoThousands": ",",
    "oPaginate": {
        "sFirst": "\u9996\u9875",
        "sPrevious": "\u4e0a\u4e00\u9875",
        "sNext": "\u4e0b\u4e00\u9875",
        "sLast": "\u5c3e\u9875",
        "sJump": "\u8df3\u8f6c"
    },
    "oAria": {
        "sSortAscending": ": \u4ee5\u5347\u5e8f\u6392\u5217\u6b64\u5217",
        "sSortDescending": ": \u4ee5\u964d\u5e8f\u6392\u5217\u6b64\u5217"
    }
};

(function (window) {
    var dateDiff = function (hisTime, nowTime) {
        var now = nowTime ? nowTime : new Date().getTime(),
			diffValue = now - hisTime,
			result = '',
			minute = 1000 * 60,
			hour = minute * 60,
			day = hour * 24,
			halfamonth = day * 15,
			month = day * 30,
			year = month * 12,
			_year = diffValue / year,
			_month = diffValue / month,
			_week = diffValue / (7 * day),
			_day = diffValue / day,
			_hour = diffValue / hour,
			_min = diffValue / minute;
        if (_year >= 1) result = parseInt(_year) + "年前";
        else if (_month >= 1) result = parseInt(_month) + "个月前";
        else if (_week >= 1) result = parseInt(_week) + "周前";
        else if (_day >= 1) result = parseInt(_day) + "天前";
        else if (_hour >= 1) result = parseInt(_hour) + "小时前";
        else if (_min >= 1) result = parseInt(_min) + "分钟前";
        else result = "刚刚";
        return result;
    };
    window.dateDiff = dateDiff;
    var cutString = function (str, len) {
        if (str.length * 2 <= len) {
            return str;
        }
        var strlen = 0;
        var s = "";
        for (var i = 0; i < str.length; i++) {
            s = s + str.charAt(i);
            if (str.charCodeAt(i) > 128) {
                strlen = strlen + 2;
                if (strlen >= len) {
                    return s.substring(0, s.length - 1) + "...";
                }
            } else {
                strlen = strlen + 1;
                if (strlen >= len) {
                    return s.substring(0, s.length - 2) + "...";
                }
            }
        }
        return s;
    }
    window.cutString = cutString;
})(window);